package com.dormitory.activity;

import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.dormitory.R;
import com.dormitory.application.MyApplication;
import com.dormitory.bean.DormitoryBean;
import com.dormitory.controller.DormitoryController;
import com.dormitory.fragment.DormitoryFragment;
import com.dormitory.fragment.SquareFragment;
import com.dormitory.fragment.UserFragment;

public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {

    private BottomNavigationBar mBottomNavigationBar;
    private Fragment mSquareFragment;
    private Fragment mDormitoryFragment;
    private Fragment mUserFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        initBottomNavigationBar();
        if (mSquareFragment == null) {
            mSquareFragment = SquareFragment.newInstance("SquareFragment", "显示广场动态");
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, mSquareFragment).commit();
        LoginAsyncTask loginAsyncTask = new LoginAsyncTask();
        loginAsyncTask.execute();
    }

    private void initBottomNavigationBar() {
        mBottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "广场"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "寝室"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "用户"))
                .initialise();
        mBottomNavigationBar.setBarBackgroundColor(R.color.bottomNavitationBarColor);
        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_SHIFTING);
        mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        mBottomNavigationBar.setTabSelectedListener(this);
    }


    @Override
    public void onTabSelected(int position) {
        switch (position) {
            case 0:
                if (mSquareFragment == null) {
                    mSquareFragment = SquareFragment.newInstance("SquareFragment", "显示广场动态");
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, mSquareFragment).commit();
                break;
            case 1:
                if (mDormitoryFragment == null) {
                    mDormitoryFragment = DormitoryFragment.newInstance("DormitoryFragment", "显示寝室");
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, mDormitoryFragment).commit();
                break;
            case 2:
                if (mUserFragment == null) {
                    mUserFragment = UserFragment.newInstance("UserFragment", "显示用户信息");
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, mUserFragment).commit();
                break;
            default:
                if (mSquareFragment == null) {
                    mSquareFragment = SquareFragment.newInstance("SquareFragment", "显示广场动态");
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, mSquareFragment).commit();
                break;
        }
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    private class LoginAsyncTask extends AsyncTask<String, Object, DormitoryBean> {

        @Override
        protected DormitoryBean doInBackground(String[] params) {
            DormitoryController dormitoryController = DormitoryController.getInstance();
            try {
                return dormitoryController.getDormitory(((MyApplication)getApplication()).getUser().getDormitoryId());
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(DormitoryBean dormitory) {
            super.onPostExecute(dormitory);
            if(dormitory == null) {
                Toast.makeText(MainActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
            }
            else
                ((MyApplication)getApplication()).setDormitory(dormitory);
        }
    }
}
